package com.yt.fras.handler;

import com.alibaba.fastjson.JSON;
import com.yt.fras.pojo.base.JsonResult;
import com.yt.fras.pojo.base.LoginUser;
import com.yt.fras.pojo.entity.Role;
import com.yt.fras.pojo.entity.User;
import com.yt.fras.service.MailService;
import com.yt.fras.service.UserRoleService;
import com.yt.fras.service.UserService;
import com.yt.fras.utils.JwtTokenUtil;
import com.yt.fras.utils.RedisUtil;
import com.yt.fras.utils.ResultTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * ClassName:CustomizeAuthenticationSuccessHandler
 * Package:com.yt.fras.handler
 * Description:
 * 登录成功处理逻辑
 *
 * @date:2021/8/15 19:54
 * @author:yt
 */

@Component
public class CustomizeAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RedisUtil redisUtil;


    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    @Autowired
    private MailService mailService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //获取登录用户
        User user = userService.queryByUsername(loginUser.getUsername());

        String device = httpServletRequest.getHeader("device");
        String version = UUID.randomUUID().toString();
        //生成jwt
        String token = jwtTokenUtil.generateToken(loginUser, version);

        if (StringUtils.isNotBlank(device) && "Android".equals(device)) {
            //将信息存入redis中
            redisUtil.hSet(user.getUsername(), "androidVersion", version, 60 * 60);
        } else {
            //将信息存入redis中
            redisUtil.hSet(user.getUsername(), "version", version, 60 * 60);
        }

        Role role = userRoleService.getRoleByUser(user.getId());
        //返回json数据
        Map<String, Object> map = new HashMap<>();
        map.put("role", role);
        map.put("token", token);
        JsonResult result = ResultTool.success();
        result.setData(map);
        //mailService.sendHtmlMail(user.getEmail(), "登录成功！", SetContent.login(user.getUsername()));
        //处理编码方式，防止中文乱码的情况
        httpServletResponse.setContentType("text/json;charset=utf-8");
        //塞到HttpServletResponse中返回给前台
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}